<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Jialeo\LaravelSchemaExtend\Schema;

class CreatePayUnionTable extends Migration
{
    private $name;
    const ORDER_BASE_NAME = 'order_pay_union';

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        $cFunc = function (Blueprint $table) {
            $table->unsignedBigInteger('id')->primary();
            $table->unsignedInteger('shopId')->default(0);
            $table->unsignedBigInteger('orderId')->default(0)->comment('orderId');
            $table->char('bizId', 32)->default("")->comment('支付流水号');
            $table->unsignedTinyInteger('payType')->default(0)->comment("1富友 2现金 3rms券 4三方券");
            $table->unsignedInteger('payPrice')->default(0)->comment('支付金额');
            $table->unsignedInteger('payStatus')->default(0)->comment('支付状态 1支付成功 2退款成功');

            $table->unsignedInteger('createdAt')->default(0)->comment('关联记录的创建时间');
            $table->unsignedInteger('originalCreatedAt')->default(0)->comment('自己的原始创建时间');

            $table->collation = 'utf8mb4_general_ci';
            $table->comment   = '支付统计表'.$this->name;

            $table->index('shopId', 'shopId_idx');
            $table->index('orderId', 'orderId_idx');
            $table->index('bizId', 'bizId_idx');
            $table->index('createdAt', 'createdAt_idx');
        };

        // 分表库
        for ($i = 0; $i <= 99; $i++) {
            $this->name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlOrder")->create(self::ORDER_BASE_NAME . $this->name, $cFunc);
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        for ($i = 0; $i <= 99; $i++) {
            $name = '_' . str_pad($i, 3, 0, STR_PAD_LEFT);
            Schema::connection("mysqlOrder")->dropIfExists(self::ORDER_BASE_NAME . $name);
        }
    }
}
